package com.listen.common;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;


public class ConfigUtils {
	
	/*INSTANCE;
	
	private ConfigUtils(){
		System.out.println("构造函数被调用");
    }*/
	
	public static Map<String,Object> resultMap = new ConcurrentHashMap<String,Object>();
	
	private static Properties properties = new Properties();
	
	private static final String SYSTEM_CONFIG_NAME = "daan.properties";
	
	/*public static void load(){
	  String filePath = null;
	  String confPath = System.getenv().get("CONFIG_PATH");
	  if(null == confPath){
		  load(SYSTEM_CONFIG_NAME);
	  }else{
		  System.out.println("进这里就读了环境变量--->"+confPath);
		  if (!confPath.endsWith(File.separator)) {
    		  confPath = confPath + File.separator;
    	  }
    	  filePath = confPath + SYSTEM_CONFIG_NAME;
    	  File file = new File(filePath);
    	  if(file.exists()){
    		  InputStream is = null;
    		  try {
    			  is = new FileInputStream(file);
    			  properties.load(is);
    		  }catch (FileNotFoundException e) {
    			  System.out.println("The config file '" + filePath + "' does not exist");
    			  e.printStackTrace();
    		  }catch (IOException e) {
    			  System.out.println("Failed to load the settings from the file: " + filePath);
    			  e.printStackTrace();
    		  }finally {
    	    	  if (is != null)
    	    		  try {is.close();
    	    		  }catch (IOException e){}
    		  }
    		  
    	  }else{
    		  load(SYSTEM_CONFIG_NAME);
    	  }
	  }
	}*/
    
	public static void load(String path){
		System.out.println("init t1 ... "+path);
		try {
			//properties.load(ConfigUtils.class.getResourceAsStream("/" + path));
			properties.load(ConfigUtils.class.getResourceAsStream("/" + path));
			Object[] objs = properties.keySet().toArray();
			for(int i=0;i<objs.length;i++){
				//System.out.println(objs[i]+" = "+properties.get(objs[i]));
				resultMap.put(objs[i]+"", properties.get(objs[i]).toString());
			}
			
		} catch (FileNotFoundException e) {
			System.out.println("The config file core-config.properties does not exist.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Failed to load the settings from the file: core-config.properties");
			e.printStackTrace();
	    }
	}
	
	static {
    	load(SYSTEM_CONFIG_NAME);
    }
	
    public static String getConfig(String key) {
    	return properties.getProperty(key);
    }
    
    public static void main(String args[]){
    	System.out.println(ConfigUtils.getConfig("575"));
    }
}
